#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
//#include "example/header/triangle_v2.hpp"
//#include "example/header/triangle_v3.h"
//#include "example/header/tex_rect_v1.hpp"
//#include "example/header/rotate_rect.hpp"
//#include "example/header/rect_3d.hpp"
#include "example/header/cube.hpp"

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

float global_var3 = 0;
double last_xpos = 400, last_ypos = 300;
float yaw = 270, pitch = 0;

int processInput(GLFWwindow *window);
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
GLFWwindow* initWindow();


int main(){
    GLFWwindow* window = initWindow();
    if (!window){
        glfwTerminate();
        return -1;
    }
//    TriangleV3 triangle;
//    TexRectV1 texRect;
//    RotateRect rect;
    Cube cube;
    bool result = cube.init(SCR_WIDTH, SCR_HEIGHT);
    if (!result){
        glfwTerminate();
        return -1;
    }
    float var1 = 0.0f, var2 = 0.0f, var3 = 0.0f;
    while (!glfwWindowShouldClose(window)) {
        int key = processInput(window);
        switch (key) {
            case GLFW_KEY_UP:{
                var1 += 0.1;
            } break;
            case GLFW_KEY_DOWN: {
                var1 -= 0.1;
            } break;
            case GLFW_KEY_RIGHT: {
                var2 += 0.1;
            } break;
            case GLFW_KEY_LEFT: {
                var2 -= 0.1;
            } break;
            default:
                break;
        }
        var3 = global_var3;
//        std::cout << "tran = " << tran << std::endl;
        cube.drawSelf(window, var1, var2, var3, yaw, pitch);
        glfwPollEvents();
    }
    cube.destroy();
    glfwTerminate();
    return 0;
}

int processInput(GLFWwindow *window){
    int key = glfwGetKey(window, GLFW_KEY_ESCAPE);
    if (key == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
    key = glfwGetKey(window, GLFW_KEY_UP);
    if (key == GLFW_PRESS) {
        return GLFW_KEY_UP;
    }
    key = glfwGetKey(window, GLFW_KEY_DOWN);
    if (key == GLFW_PRESS) {
        return GLFW_KEY_DOWN;
    }
     key = glfwGetKey(window, GLFW_KEY_RIGHT);
    if (key == GLFW_PRESS) {
        return GLFW_KEY_RIGHT;
    }
    key = glfwGetKey(window, GLFW_KEY_LEFT);
    if (key == GLFW_PRESS) {
        return GLFW_KEY_LEFT;
    }

    return -1;
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height){
    glViewport(0, 0, width, height);
}

void scroll_callback(GLFWwindow* window, double xoffset, double yoffset){
    if (yoffset > 0){
        global_var3 += 0.1f;
    } else {
        global_var3 -= 0.1f;
    }
}

void mouse_callback(GLFWwindow* window, double xpos, double ypos){
//    std::cout << "xpos:" << xpos << ", ypos:" << ypos << std::endl;
    float xoffset = xpos - last_xpos;
    float yoffset = last_ypos - ypos; // 注意这里是相反的，因为y坐标是从底部往顶部依次增大的
    last_xpos = xpos;
    last_ypos = ypos;

    float sensitivity = 0.05f;
    xoffset *= sensitivity;
    yoffset *= sensitivity;
    
    yaw   += xoffset;
    pitch += yoffset;
    
    if(pitch > 89.0f)
      pitch =  89.0f;
    if(pitch < -89.0f)
      pitch = -89.0f;
}

GLFWwindow* initWindow(){
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return 0;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    glfwSetScrollCallback(window, scroll_callback);
    glfwSetCursorPosCallback(window, mouse_callback);
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return 0;
    }
    return window;
}
